<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/17
 * Time: 14:19
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\BoxModel;
use App\Http\Model\Mysql\BoxAreaModel;
use App\Http\Model\Mysql\BoxOwnerModel;
use App\Http\Model\Mysql\ShopModel;
use Carbon\Carbon;

class BoxService extends BaseService
{
    /**
     * 盒子列表
     * @param array $query
     * @param int|null $limit
     * @param int $page
     * @return array
     */
    public function boxList(array $query = [], int $limit = null, int $page = 1)
    {
        $where['is_delete']  =   BoxModel::NOT_DELETE;

        if (!empty($query['owner_id'])) {
            $where['owner_id']  =   $query['owner_id'];
        }

        if (!empty($query['area_id'])) {
            $where['area_id']   =   $query['area_id'];
        }

        if (!empty($query['shop_id'])) {
            $where['shop_id']   =   $query['shop_id'];
        }

        if (!empty($query['box_name'])) {
            $where[]    =   ['box_name', 'like', '%' . $query['box_name'] . '%'];
        }

        $limit  =   empty($limit) ? self::PAGE_LIMIT : $limit;
        $count  =   BoxModel::with([
                        'owner' =>  function ($query) {
                            $query->select(['id', 'name', 'phone']);
                        }
                    ])->where($where)->count();
        $list   =   BoxModel::with([
                        'owner' =>  function ($query) {
                            $query->select(['id', 'name', 'phone']);
                        }
                    ])->where($where)->limit($limit)->offset(($page - 1) * $limit)->select(['id', 'unique_id', 'box_name', 'box_code', 'owner_id', 'remark', 'created_at', 'area_id', 'is_online'])->get()
                    ->each(function (&$item) {
                        $item->created_time =   $item->created_at->toDateTimeString();
                        unset($item->created_at);
                        $item->area_name    =   BoxAreaModel::where(['id' => $item->area_id])->value('area_name') ?? '';
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'list'  =>  $list->toArray(),
                'pages' =>  ceil($count / $limit),
                'current'=> $page
            ]];
        }
    }

    /**
     * 盒子添加
     * @param array $params
     * @return array
     */
    public function boxAdd(array $params)
    {
        $insert['unique_id']=   createUniqueID();
        $insert['box_name'] =   $params['box_name'];
        $insert['box_code'] =   $params['box_code'];
        $insert['user_id']  =   $params['user_id'] ?? 0;
        $insert['owner_id'] =   $params['owner_id'];
        $insert['shop_id']  =   $params['shop_id'];
        $insert['parent_id']=   $params['parent_id'] ?? 0;
        $insert['area_id']  =   $params['area_id'];
        $insert['remark']   =   $params['remark'] ?? '';
        $insert['is_online']=   1;

        $insert_result  =   BoxModel::create($insert);
        if ($insert_result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * @param array $params
     * @param int $box_id
     * @return array
     */
    public function boxEdit(array $params, int $box_id)
    {
        $update['box_name'] =   $params['box_name'];
        $update['box_code'] =   $params['box_code'];
        $update['user_id']  =   $params['user_id'] ?? 0;
        $update['owner_id'] =   $params['owner_id'];
        $update['shop_id']  =   $params['shop_id'];
        $update['parent_id']=   $params['parent_id'] ?? 0;
        $update['area_id']  =   $params['area_id'];
        $update['remark']   =   $params['remark'] ?? '';

        $update_result  =   BoxModel::where(['id' => $box_id])->update($update);
        if ($update_result) {
            return [true, '修改成功'];
        } else {
            return [false, '修改失败'];
        }
    }

    /**
     * 删除盒子
     * @param int $box_id
     * @return array
     */
    public function boxDelete(int $box_id)
    {
        $delete_result  =   BoxModel::where(['id' => $box_id])->update(['is_delete' => BoxModel::IS_DELETE, 'delete_time' => Carbon::now()->timestamp]);
        if ($delete_result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 盒子详情
     * @param int $box_id
     * @return array
     */
    public function boxInfo(int $box_id)
    {
        $info  =   BoxModel::where(['id' => $box_id, 'is_delete' => BoxModel::NOT_DELETE])->select(['id', 'unique_id', 'box_name', 'box_code', 'shop_id', 'owner_id', 'area_id', 'remark', 'is_online'])->first();
        if (!empty($info)) {
            $info->owner_name   =   BoxOwnerModel::where(['id' => $info->owner_id])->value('name') ?? '';
            $info->shop_name    =   ShopModel::where(['id' => $info->shop_id])->value('shop_name') ?? '';
            return [true, $info->toArray()];
        } else {
            return [false, '盒子不存在'];
        }
    }

}
